package com.ihd.ihardware.common.utils;

import java.math.BigDecimal;

public class CalUtils {
    public static BigDecimal getREE(int sex, String weight, String height, int age) {
        BigDecimal w = new BigDecimal(weight);
        BigDecimal h = new BigDecimal(height);
        BigDecimal a = new BigDecimal(age);

        if (sex == 1) {
            return w.multiply(new BigDecimal(10)).add(h.multiply(new BigDecimal(6.25))).subtract(a.multiply(new BigDecimal(5))).add(new BigDecimal(5));
        } else {
            return w.multiply(new BigDecimal(10)).add(h.multiply(new BigDecimal(6.25))).subtract(a.multiply(new BigDecimal(5))).subtract(new BigDecimal(161));
        }
    }

    public static String getTS(int sex, String weight, String height, int age) {
        return getREE(sex, weight, height, age).multiply(new BigDecimal(0.2084)).multiply(new BigDecimal(0.5998)).setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString();
    }

    public static String getZF(int sex, String weight, String height, int age) {
        return getREE(sex, weight, height, age).multiply(new BigDecimal(0.2084)).multiply(new BigDecimal(0.1599)).setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString();
    }

    public static String getDBZ(int sex, String weight, String height, int age) {
        return getREE(sex, weight, height, age).multiply(new BigDecimal(0.2084)).multiply(new BigDecimal(0.2402)).setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString();
    }

    /**
     * 体脂率=(1.898*BMI-23.4)*0.95
     *
     * @param BMI
     * @return
     */
    public static BigDecimal getFat(BigDecimal BMI) {
        return new BigDecimal(1.898).multiply(BMI).subtract(new BigDecimal(23.4))
                .multiply(new BigDecimal(0.95)).abs().setScale(2, BigDecimal.ROUND_HALF_EVEN);
    }

    /**
     * BMI描述
     *
     * @return
     */
    public static int getBMIDes(boolean woman, BigDecimal BMI) {
        // 男 BMI_DESCRIPTION 18.5,20.5,23.0,25.5,29.9
        if (!woman) {
            //瘦<19.0
            if (-1 == BMI.compareTo(new BigDecimal(18.5))) {
                return 1;
            }
            //偏瘦19.0~20.9
            if (0 == BMI.compareTo(new BigDecimal(18.5)) || 0 >= BMI.compareTo(new BigDecimal(20.5))) {
                return 2;
            }
            //标准21.0~23.9
            if (0 == BMI.compareTo(new BigDecimal(20.5)) || 0 >= BMI.compareTo(new BigDecimal(23.0))) {
                return 2;
            }
            //偏胖24.0~25.4
            if (0 == BMI.compareTo(new BigDecimal(23.0)) || 0 >= BMI.compareTo(new BigDecimal(25.5))) {
                return 3;
            }
            //肥胖25.5~29.9
            if (0 == BMI.compareTo(new BigDecimal(25.5)) || 0 >= BMI.compareTo(new BigDecimal(29.9))) {
                return 4;
            }
            //极度肥胖>29.9
            if (1 == BMI.compareTo(new BigDecimal(29.9))) {
                return 5;
            }
        }
        // 女 19.0,21.0,24.0,25.5,29.9
        else {
            //瘦<18.5
            if (-1 == BMI.compareTo(new BigDecimal(19.0))) {
                return 1;
            }
            //偏瘦18.6~20.4
            if (0 == BMI.compareTo(new BigDecimal(19.0)) || 0 >= BMI.compareTo(new BigDecimal(21.0))) {
                return 2;
            }
            //标准20.5~22.9
            if (0 == BMI.compareTo(new BigDecimal(21.0)) || 0 >= BMI.compareTo(new BigDecimal(24.0))) {
                return 2;
            }
            //偏胖23.0~25.4
            if (0 == BMI.compareTo(new BigDecimal(24.0)) || 0 >= BMI.compareTo(new BigDecimal(25.5))) {
                return 3;
            }
            //肥胖25.5~29.9
            if (0 == BMI.compareTo(new BigDecimal(25.5)) || 0 >= BMI.compareTo(new BigDecimal(29.9))) {
                return 4;
            }
            //极度肥胖>29.9
            if (1 == BMI.compareTo(new BigDecimal(29.9))) {
                return 5;
            }
        }
        return 0;
    }
}
